﻿<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<DTO.Models.MEDIA>>" %>


<div class="dung-left">
	
</div>        
<div class="dung-right">
    <%--<%= Html.ActionLink("Thêm mới","GetForm","Media", new {ID = 0, TypeMediaName= ViewData["TypeMediaName"] }, new {@class="dung-button"}) %>--%>
    <input id="btnAddNew" type="button" class="dung-button" onclick="addnew()" value="Thêm mới" />
    <input id="btnRemoveList" type="button" class="dung-button" onclick="removemedia()" value="Xóa" />
</div>
<div class="clearer">&nbsp;</div>
<div>
	<form action="" method="post" id="postlist">
		<table width="100%" class="dung-table">
			<tr>
				<th width="5%"></th>
				<th width="15%">Tên</th>
				<th width="18%">Giới thiệu</th>
				<th width="15%">Hình ảnh</th>
                <th width="10%">Ngày tạo</th>
                <th width="8%">Ngày update</th>
                <th width="7%">Người tạo</th>
                <th width="7%">Người update</th>
				<th width="15%">Công cụ</th>
			</tr>
			<tbody>
                <%
                    foreach (var obj in Model)
                    {
                        
                %>
                        <tr>
                            <td>
                                <%= Html.CheckBox("mediaidselect", false, new {value = obj.ID}) %>
                            </td>
                            <td><%= Html.Encode(obj.Title) %></td>
                            <td><%= Html.Encode(obj.Intro) %></td>
                            <td><img alt="" src="<%= Url.Content("~/MediaImage/" + obj.ID.ToString() + ".jpg") %>" class="png" width="100" height="80" /></td>
                            <td>
                                <%= Html.Encode(string.Format("{0:dd/MM/yyyy H:mm:ss tt}", obj.DateCreate)) %>
                            </td>
                            <td>
                                <%= Html.Encode(string.Format("{0:dd/MM/yyyy H:mm:ss tt}", obj.DateModify)) %>
                            </td>
                            <td><%= Html.Encode(obj.UserCreate) %></td>
                            <td><%= Html.Encode(obj.UserModify) %></td>
                            <td>
                                <%= Html.ActionLink("Cập nhật","GetForm","Media", new {ID = obj.ID, TypeMediaName= ViewData["TypeMediaName"] }, new {@class="dung-button"}) %>
                                <br/>
                                <br/>
                                <% if (obj.IsTop.Value)
                                   { %>
                                <%= Html.ActionLink("Đưa xuống", "DeActive", "Media", new { ID = obj.ID, TypeMediaName = ViewData["TypeMediaName"] }, new { @class = "dung-button" })%>
                                <%} else { %>
                                <%= Html.ActionLink("Đưa lên", "Active", "Media", new { ID = obj.ID, TypeMediaName = ViewData["TypeMediaName"] }, new { @class = "dung-button" })%>
                                <%} %>
                            </td>
                        </tr>
                <%         
                    }
                %>
			</tbody>
		</table>
	</form>
</div>
<div>
    <div class="dung-right">
    <% 
        int totalpage = Convert.ToInt32(ViewData["TotalPage"]);
        int pageindex = Convert.ToInt32(ViewData["PageIndex"]);
        int pagesize = Convert.ToInt32(ViewData["PageSize"]);
        
            
        int th = pageindex / pagesize;
        int begin = 1;
        int max = totalpage;
        if(th - 1 > 0)
        {
            begin = (th - 1) * pagesize;
        }
        if((th + 1) * pagesize < totalpage)
        {
            max = (th + 1) * pagesize;   
        }
    %>
    <% Response.Write("<input type='button' class='dung-button' onclick='loadmediapage(" + 1 + ")' value='|<' />");%>
    <%    
        for(int index = begin; index <= max; index++)
        {
            if(index != pageindex)
            {
                if((index == begin && index % pagesize == 0) || (index == max && index % pagesize == 0))
                {
                    %>
                    <% Response.Write("<input type='button' class='dung-button' onclick='loadmediapage("+index.ToString()+")' value='...' />"); %>
                    <%
                }
                else
                {
                    %>
                    <% Response.Write("<input type='button' class='dung-button' onclick='loadmediapage(" + index.ToString() + ")' value='" + index.ToString() + "' />"); %>
                    <%
                }
            }    
            else
            {
                %>
                <% Response.Write("<input type='button' class='dung-button-other' onclick='loadmediapage("+index.ToString()+")' value='" + index.ToString() + "' />"); %>
                <%
            }
        }
    %>
	<% Response.Write("<input type='button' class='dung-button' onclick='loadmediapage(" + (totalpage).ToString() + ")' value='>|' />");%>
    
</div>
<div class="clearer">&nbsp;</div>
</div>

<script type="text/javascript">
    
    function addnew()
    {
        window.location = "/Media/GetForm/0?TypeMediaName=" + "<%= ViewData["TypeMediaName"].ToString() %>";
    }

    function removemedia() {
        var listid = "";
        $("input:checked").each(function () {
            listid += $(this).val() + "-";
        });
        if(listid.length == 0){
            confirm("Vui lòng chọn một item để xóa");
        }
        else
        {
            if(confirm("Bạn có chắc muốn xóa không?") == true)
            {
                $.get("/Media/Delete/"+ listid, 
                    function (data) {
                        loadmediapage(<%= ViewData["PageIndex"] %>);
                    });
            }
        }
    }
    
    function loadmediapage(pageindex) {
        
        var status = $("#statuslist").val() + "";
        if (status.indexOf("all") == -1) {
            search(pageindex);
        }
        else {
            viewall(pageindex);
        }

    }
</script>

